package com.example.secondhandshop.ui.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.secondhandshop.R;
import com.example.secondhandshop.model.RechargeModel;

public class MoneyActivity extends AppCompatActivity {

    private TextView moneyTextView;
    public static int  newMoney;
    public int money;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_money);
        moneyTextView=findViewById(R.id.moneyText);
        SharedPreferences sharedPreferences = getSharedPreferences(
                getResources().getString(R.string.sharepreferences_filename),
                Context.MODE_PRIVATE
        );
        money = sharedPreferences.getInt("money", 0); // 默认值为0
        moneyTextView.setText("￥"+money);
        Button rechargeButton = findViewById(R.id.rechargeButton);
        rechargeButton.setOnClickListener(v -> showRechargeAmountInputDialog());
    }

    private void showRechargeAmountInputDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(MoneyActivity.this);
        builder.setTitle("输入充值金额");

        final EditText input = new EditText(MoneyActivity.this);
        input.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        builder.setView(input);

        builder.setPositiveButton("充值", (dialog, which) -> {
            String amount = input.getText().toString();
            try {
               int rechargeAmount = Integer.parseInt(amount);
                Log.d("Recharge", "用户输入的金额：" + rechargeAmount);
                    long userId = LoginAcitivity.userid;

                if (rechargeAmount > 0) {
                    RechargeModel model = new RechargeModel();
                    model.recharge(rechargeAmount, userId, new RechargeModel.OnRechargeListener() {
                        @Override
                        public void onSuccess() {
                            runOnUiThread(() -> {
                                newMoney=money+rechargeAmount;
                                SharedPreferences sharedPreferences = getSharedPreferences(getResources().getString(R.string.sharepreferences_filename), MODE_PRIVATE);
                                SharedPreferences.Editor editor = sharedPreferences.edit();
                                editor.putInt("money",newMoney); // 存储 money
                                editor.apply();
                                moneyTextView.setText("￥"+newMoney);
                                Toast.makeText(MoneyActivity.this, "充值成功", Toast.LENGTH_SHORT).show();
                            });
                        }

                        @Override
                        public void onFailure() {
                            runOnUiThread(() -> Toast.makeText(MoneyActivity.this, "充值失败", Toast.LENGTH_SHORT).show());
                        }

                        @Override
                        public void onError() {
                            runOnUiThread(() -> Toast.makeText(MoneyActivity.this, "error", Toast.LENGTH_SHORT).show());
                        }
                    });
                } else {
                    Toast.makeText(MoneyActivity.this, "请输入有效的充值金额", Toast.LENGTH_SHORT).show();
                }
            } catch (NumberFormatException e) {
                Toast.makeText(MoneyActivity.this, "请输入有效的数字", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消", (dialog, which) -> dialog.dismiss());
        builder.show();
    }
    private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            int nowmoney = intent.getIntExtra("monney",0);
            if(nowmoney!=0){
                newMoney=nowmoney;
            }
            // 处理数据
        }
    };

    @Override
    public void onResume() {
        super.onResume();
        IntentFilter filter = new IntentFilter("MoneyActivity");
        registerReceiver(receiver, filter);
    }

    public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
        return null;
    }

    @Override
    public void onPause() {
        super.onPause();
        unregisterReceiver(receiver);
    }

    public void unregisterReceiver(BroadcastReceiver receiver) {

    }
}